package com.cheng.frame;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import com.cheng.listener.CreateCodeListener;
import com.cheng.util.DataUtil;


public class CreateCodeFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private static CreateCodeFrame frame;
	
	private TicketExPanel ticketExPanel;
	private InfoShowPanel infoShowPanel;
	private CodeViewPanel codeViewPanel;
	private JPanel jPanel1;
	private JPanel jPanel2;
	private JButton bLast1;
	private JButton bLast2;
	private JButton bCreate;
	private JButton bPrint;
	private JButton bKeep;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	private CreateCodeFrame() {
		frame = this;
		initComponents();
		frame.setDefaultCloseOperation(CreateCodeFrame.EXIT_ON_CLOSE);
		frame.setTitle("火车票实名认证系统");
		frame.getContentPane().setPreferredSize(frame.getSize());
		frame.pack();
		frame.setLocationRelativeTo(null);
	}
	
	public static CreateCodeFrame instance(){
		if(frame == null)
			frame = new CreateCodeFrame();
		return frame;
	}

	private void initComponents() {
		setTitle("火车票实名认证系统");
		setLayout(new GroupLayout());
		ticketExPanel = new TicketExPanel().getPanel();
		infoShowPanel = new InfoShowPanel().getPanel();
		codeViewPanel = new CodeViewPanel().getPanel();
		add(ticketExPanel, new Constraints(new Leading(530, 250, 10, 10), new Leading(10, 250, 10, 10)));
		add(infoShowPanel, new Constraints(new Leading(20, 470, 10, 10), new Leading(10, 250, 10, 10)));
		add(codeViewPanel, new Constraints(new Leading(20, 760, 10, 10), new Leading(270, 250, 10, 10)));
		add(getJPanel1(), new Constraints(new Leading(410, 370, 10, 10), new Leading(510, 100, 10, 10)));
		add(getJPanel2(), new Constraints(new Leading(310, 470, 10, 10), new Leading(510, 100, 10, 10)));
		setSize(800, 600);
		setIconImage(DataUtil.instance().getIcon());
		setResizable(false);
	}

	private JButton getBCreate() {
		if (bCreate == null) {
			bCreate = new JButton();
			bCreate.setText("制证");
			bCreate.setDefaultCapable(true);
			bCreate.addActionListener(new CreateCodeListener(bLast1,bCreate,bKeep,bPrint));
		}
		return bCreate;
	}

	private JButton getBLast1() {
		if (bLast1 == null) {
			bLast1 = new JButton();
			bLast1.setText("返回");
			bLast1.setDefaultCapable(true);
			bLast1.addActionListener(new CreateCodeListener(bLast1,bCreate,bKeep,bPrint));
		}
		return bLast1;
	}
	
	private JButton getBLast2() {
		if (bLast2 == null) {
			bLast2 = new JButton();
			bLast2.setText("返回");
			bLast2.setDefaultCapable(true);
			bLast2.addActionListener(new CreateCodeListener(bLast2,bCreate,bKeep,bPrint));
		}
		return bLast2;
	}
	
	private JButton getBPrint() {
		if (bPrint == null) {
			bPrint = new JButton();
			bPrint.setText("打印");
			bPrint.setDefaultCapable(true);
			bPrint.addActionListener(new CreateCodeListener(bLast2,bCreate,bKeep,bPrint));
		}
		return bPrint;
	}
	
	private JButton getBKeep() {
		if (bKeep == null) {
			bKeep = new JButton();
			bKeep.setText("保存");
			bKeep.setDefaultCapable(true);
			bKeep.addActionListener(new CreateCodeListener(bLast2,bCreate,bKeep,bPrint));
		}
		return bKeep;
	}

	public JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GroupLayout());
			jPanel1.add(getBCreate(), new Constraints(new Leading(60,100,10,10), new Leading(25,50,10,10)));
			jPanel1.add(getBLast1(), new Constraints(new Leading(220,100,10,10), new Leading(25,50,10,10)));
		}
		return jPanel1;
	}
	
	public JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GroupLayout());
			jPanel2.add(getBKeep(), new Constraints(new Leading(0,100,10,10), new Leading(25,50,10,10)));
			jPanel2.add(getBPrint(), new Constraints(new Leading(160,100,10,10), new Leading(25,50,10,10)));
			jPanel2.add(getBLast2(), new Constraints(new Leading(320,100,10,10), new Leading(25,50,10,10)));
			jPanel2.setVisible(false);
		}
		return jPanel2;
	}

	public InfoShowPanel getInfoShowPanel() {
		return infoShowPanel;
	}

	public CodeViewPanel getCodeViewPanel() {
		return codeViewPanel;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				CreateCodeFrame frame = CreateCodeFrame.instance();
				frame.setDefaultCloseOperation(CreateCodeFrame.EXIT_ON_CLOSE);
				frame.setTitle("火车票实名认证系统");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

}
